package Game;

import java.util.HashMap;
import java.util.Map;

import Unite.Unit;

public class PairingTable {
	private Map<Integer, Map<Integer, Double>> _pairingTable;

	/**
	 * 
	 */
	public PairingTable() {
		_pairingTable = new HashMap<Integer, Map<Integer, Double>>();
	}

	/**
	 * @param unitCurrent
	 * @param unitOpponent
	 * @param weight
	 */
	public void pair(Unit unitCurrent, Unit unitOpponent, double weight) {
		System.out.println("pairing: #" + unitCurrent.getId() + "("
				+ unitCurrent.getClasseId() + ")" + " and #"
				+ unitOpponent.getId() + "(" + unitCurrent.getClasseId() + ")"
				+ " with value:" + weight);
		this.set(unitCurrent.getId(), unitOpponent.getId(), weight);
	}

	/**
	 * @param unitCurrentId
	 * @param unitOpponentId
	 * @return
	 */
	double get(int unitCurrentId, int unitOpponentId) {
		return _pairingTable.get(unitCurrentId).get(unitOpponentId);
	}

	/**
	 * @param unitCurrentId
	 * @param unitOpponentId
	 * @param value
	 */
	public synchronized void set(int unitCurrentId, int unitOpponentId,
			double value) {
		if (!_pairingTable.containsKey(unitCurrentId))
			_pairingTable.put(unitCurrentId, new HashMap<Integer, Double>());
		_pairingTable.get(unitCurrentId).put(unitOpponentId, value);
	}

	/**
	 * @param unitCurrent
	 * @return
	 */
	public double getDamage(Unit unitCurrent) {
		int damage = 0;
		for (int unitOpponentId : _pairingTable.get(unitCurrent.getId())
				.keySet()) {
			damage += get(unitCurrent.getId(), unitOpponentId);
		}

		return damage;
	}

	/**
	 * 
	 */
	public void print() {
		System.out.println(_pairingTable.toString());
		for (Integer currentUnitId : _pairingTable.keySet()) {
			System.out.print("Damage of unit #" + currentUnitId + " to : ");
			for (Integer otherUnitId : _pairingTable.get(currentUnitId)
					.keySet()) {
				System.out.print("(#" + otherUnitId + ":"
						+ _pairingTable.get(currentUnitId).get(otherUnitId)
						+ ")");
			}
			System.out.println("");
		}
	}

	/**
	 * @param attackingUnit
	 * @param defendingUnit
	 * @return
	 */
	public double getRatio(Unit attackingUnit, Unit defendingUnit) {
		return _pairingTable.get(attackingUnit.getId()).get(
				defendingUnit.getId());
	}
}
